<?php
if ($ticket_parent!=1) {
	header('Location: class_manager.php');
	exit;
}

?>

<script type="text/javascript">
	function expand_collapse_div(element_id){
		var t=document.getElementById(element_id);
		if(!t)return false;
		if(t.style.display=="none"){
			t.style.display="block";
		}
		else{
			t.style.display="none";
		}
		return true;
	}
</script>







<h2 class="spaced">Welcome</h2>
<div class="present"><p>
Welcome the Agony Unleashed Class Manager.  This tool will allow you to check out upcoming classes, register for them, and monitor your registration status.
</p>
<div class="spaced">Please be aware of the following:</div>
<ul id="intro_list">
<li class="intro_list_element"> You <b>must</b> have javascript enabled for this tool to work properly.  If portions of this tool do not work as expected, make sure that your browser supports javascript and that it is enabled!
<li class="intro_list_element"> All times are Eve Time.
<li class="intro_list_element"> In order to register for classes, you must <a href="http://www.agony-unleashed.com/index.php?app=core&module=global&section=login">login</a> to your Agony Unleashed web account.
</ul>
</div>



<?php 
$result = mysql_query("select ordered_classes.pkey, UNIX_TIMESTAMP(ordered_classes.reg_start) as reg_start, ordered_classes.capacity, ordered_classes.fee, ordered_classes.comments, UNIX_TIMESTAMP(ordered_classes.start_datetime) as start_datetime, modules.pkey as module, modules.short_name, modules.long_name from (select * from (select classes.pkey as pkey, classes.module, classes.reg_start, classes.capacity, classes.fee, classes.comments, sessions.start_datetime from classes left join sessions on classes.pkey=sessions.class where classes.cancelled=0 AND sessions.start_datetime > NOW() order by start_datetime asc) class_w_startdate group by pkey order by start_datetime asc) ordered_classes left join modules on ordered_classes.module=modules.pkey", $class_manager_db) or die(mysql_error($class_manager_db));
if(mysql_num_rows($result) <= 0){
?>
<div class="present">
<h2 class="spaced">No Upcoming Classes - Check Back Soon</h2>
</div>
<?php
} 
else{
?>
<div class="present">
<h2 class="spaced">Upcoming Classes</h2>
Click a class to expand it for details.
<table>
<?php 
$i=0;
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
	
	if($line["start_datetime"] == NULL)
		$class_header=htmlspecialchars(stripslashes($line["long_name"]))." (???)";
	else
		$class_header=htmlspecialchars(stripslashes($line["long_name"]))." (".date("d M Y",$line["start_datetime"]).")";
?>
	<tr><td colspan="3"><hr></td></tr>
	<tr><td colspan="3">
		<h3><a onclick="expand_collapse_div('class_details_<?php echo $i;?>')" class="clickable">
			<?php echo $class_header;?>
		</a></h3>
	</td></tr>
	<tr><td colspan="3">
		<div id="class_details_<?php echo $i;?>" style="display:none;">







			<table class="present">
				<tr>
					<?php 
					if($line["reg_start"] == NULL || $line["reg_start"]<time()){
						$participants_result = mysql_query("SELECT pkey FROM participants WHERE class='".$line["pkey"]."' AND (status='registered' OR status='wait_list')", $class_manager_db) or die(mysql_error($class_manager_db));
					?>
						<td colspan="4">Registration is open!<br><b><?php echo mysql_num_rows($participants_result);?></b> students have signed up.</td>
					<?php
					} 
					else{
					?>
						<td align="right">Registration Opens:</td>
						<td colspan="3"><b><?php echo date("d M Y H:i",$line["reg_start"]);?></b></td>
					<?php
					}
					?>
				</tr>
				<tr>
					<td align="right">Fee:</td>
					<td align="left"><?php echo number_format($line["fee"],2,".",",");?> ISK</td>
					<td align="right">Capacity:</td>
					<td align="left"><?php if($line["capacity"] == NULL) echo 'Unlimited'; else echo $line["capacity"];?></td>
				</tr>
				<tr>
					<td align="left" colspan="4"><?php echo htmlspecialchars(stripslashes($line["comments"]));?></td>
				</tr>
				<tr>
					<td valign="top" align="right"><h4>Class Sessions</h4></td>
					<td colspan="3">
					
					
					
					
					
					
					
					

							<table class="subform">
								<?php 
								$j=0;
								$session_result = mysql_query("SELECT pkey, UNIX_TIMESTAMP(start_datetime) as start_datetime, duration, comments 
									FROM sessions WHERE class=".$line["pkey"]." ORDER BY start_datetime ASC", $class_manager_db) or die(mysql_error($class_manager_db));
								while ($session_line = mysql_fetch_array($session_result, MYSQL_ASSOC)) {
								?>
									<tr>
										<td><hr></td>
									</tr>
									<tr>
										<td>Session <?php echo ($j+1);?> starts <b><?php echo date("d M Y H:i",$session_line["start_datetime"]);?></b> and will last for <b><?php echo $session_line["duration"];?></b> hour(s).</td>
									</tr>
									<tr>
										<td align="left">
											<?php echo htmlspecialchars(stripslashes($session_line["comments"]));?>
										</td>
									</tr>
								<?php
								$j++;
								}
								?>
									<tr>
										<td><hr></td>
									</tr>
							</table>
						
						
						
						
						
						
						
						
					</td>
				</tr>
				
				
				
				
				
				
				
<?php 
if(is_logged_in() && ($line["reg_start"] == NULL || $line["reg_start"] <= time())){
?>				
				<tr>
					<td align="left" colspan="4"><h4>Register</h4></td>
				</tr>
				<?php 
				$j=0;
				$prereq_result = mysql_query("SELECT * FROM prerequisites LEFT JOIN modules ON prerequisites.prerequisite=modules.pkey WHERE prerequisites.module='".$line["module"]."' ORDER BY modules.short_name ASC", $class_manager_db) or die(mysql_error($class_manager_db));
				if(mysql_num_rows($prereq_result)>0) {
				?>
				<tr>
					<td align="left" colspan="4">Prerequisites: 
					<?php 
					$j=0;
					while ($prereq_line = mysql_fetch_array($prereq_result, MYSQL_ASSOC)) {
						if($j>0)
							echo ", ";
						echo htmlspecialchars($prereq_line["long_name"])." (".htmlspecialchars($prereq_line["short_name"]).")";
						$j++;
					}
					?>
					
					</td>
				</tr>
				<?php 
				}
				?>
				<tr>
					<td colspan="4">
						<form name="register_class_form"
							action="class_manager.php?page=upcoming"
							method="post">
						<table style="width:100%;">
							<tr>
								<td align="right" style="width:125px;">Character Name:</td>
								<td align="left"><input type="text" style="width:300px" name="register_class_form_char_name">&nbsp;<input type="checkbox" name="register_class_form_alumnus" value="1">&nbsp;Alumnus</td>
								<td align="right"><input type="hidden" name="register_class_form_class" value="<?php echo $line["pkey"];?>"><input type="submit" name="register_class_form_submit" value="Register"></td>
							</tr>
						</table>
						</form>
					</td>
				</tr>
				<tr>
					<td align="left" colspan="4"><b>Important:</b> The above should be the in-game name of the character you are registering, please make sure this is exact!<br>You may register for a class even if it is full, however you will be placed on the waiting list.</td>
				</tr>
<?php 
}
?>







			</table>
				
				
				
				
				
				
				
				
		</div>
	</td></tr>
<?php
	$i++;
}
?>
	<tr><td colspan="3"><hr></td></tr>

</table>
</div>
<?php 
}
?>